<template>
  <div class="login-container">
    <el-form ref="ruleFormRef" :model="form" :rules="rules">
      <div class="title-container">
        <h3 class="title">用户登录</h3>
      </div>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="form.userName" />
      </el-form-item>
      <el-form-item label="密  码" prop="password">
        <el-input v-model="form.password" />
      </el-form-item>
      <el-button type="primary" @click="handlerLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useStore } from "vuex";
const myStore = useStore();
const ruleFormRef = ref(null);
const form = ref({
  userName: "d",
  password: "123",
});

const rules = ref({
  userName: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

const handlerLogin = () => {
  ruleFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      myStore.dispatch("appLogin", form.value);
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>

<style scoped>
.el-form {
  width: 300px;
  margin: 100px auto;
  padding: 20px;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 10px #ccc;
  border-radius: 5px;
  background-color: #fff;
}

.el-form-item__label {
  text-align: left;
}

.el-button--primary {
  width: 100%;
}
</style>
